{% extends base %}

{% block head %}
<script type="text/javascript" src="/static/lib/csv.js/csv.js"></script>
<script type="text/javascript" src="/static/lib/marked/marked.js"></script>
<script type="text/javascript" src="/static/js/marked-ext.js"></script>
<style>
.CodeMirror {
    width: 60rem;
    font-size:16px;
    height: auto;
}

#markdown-input {
    font-family: monospace;
}

.marked-img {
    text-align: center;
}

.small {
    font-size: small;
}

img {
    max-width: 100%;
}

.alert {
    color: red;
    background-color: #FFCCCC;
    width: initial;
}

.x-code-block {
    font-family: Consolas,monospace;
    word-wrap: break-word;
    border-style: solid;
    border-radius: 3px;
    border-width: 1px;
    padding: 3px;
    background-color: #ffffcc;
}

.bottom-fixed-pannel {
    background-color: white; 
    position: fixed; 
    padding-bottom: 5px; 
    padding-top:5px; 
    bottom: 0px; 
    z-index: 100
}

.marked-heading {
    /* 避免顶部fixed菜单遮挡 */
    padding-top: 50px;
    margin-top: -50px;
}

.pathlist {
    background-color: #eee;
    padding:3px;
    box-sizing: border-box;
}

.hover {
    color: #AAA;
}

.hover:hover {
    color: #000;
}

.no-border tr td {
    border: none;
}

#uploadIframe {
    border: none;
    width: 100%;
    height: 60px;
}

#markdown-output-div img {
    max-height: 600px;
}

.red-border {
    border-color: red;
}

/** 分组的标签 **/
.group-tag {
    border: 1px solid #ccc;
    background-color: #69c;
    padding: 5px;
    margin: 5px;
    display: block;
    float: left;
    line-height: 20px;
    color: #fff;
}

</style>
{% end %}


{% block body %}
    {% init file_type       = "md" %}
    {% init pathlist        = [] %}
    {% init file            = None %}
    {% init show_notice     = False %}
    {% init show_search_div = True %}
    {% init show_add_file   = False %}
    {% init show_add_group  = False %}
    {% init show_path_list  = True %}
    {% init show_pagination = True %}
    {% init error           = "" %}

    {% if file != None %}
        {% set globals()['file_type'] = file.type %}
        {% set globals()['file_id'] = file.id %}
    {% end %}

    {% if show_notice %}
        {% include common/mod_notice.html %}
    {% end %}

    {# 收藏一下这个符号 › #}
    <div class="col-md-12 card">
        {% if show_path_list %}
        <div class="grid-title">
            <!-- <a href="/" class="link2">{{T("Home")}}</a> -->
            {% for i, item in enumerate(pathlist) %}
                {% if i > 0 %} / {% end %}<a href="{{item.url}}" class="link2">{{ item.name }}</a>
            {% end %}

            {% if file is not None %}
                {% if file.type == "group" and _user_name == file.creator %}
                    {# 笔记列表 #}
                    <div class="float-right">
                        <a class="link" href="/note/add?parent_id={{file.id}}">新增</a>

                        <div class="dropdown">
                          <span class="dropdown-btn link">更多▾</span>
                          <div class="dropdown-content">                            
                            {% if file.priority > 0 %}
                                <a class="dropdown-option" href="/note/unstick?id={{file.id}}">取消置顶</a>
                            {% else %}
                                <a class="dropdown-option" href="/note/stick?id={{file.id}}">置顶</a>
                            {% end %}

                            <a class="dropdown-option" href="javascript:rename('{{file.id}}', '{{file.name}}')">重命名</a>
                            <a class="dropdown-option" href="/note/view?id={{file.id}}&orderby=mtime_desc&page={{page}}">按时间排序</a>
                            <a class="dropdown-option" href="/note/view?id={{file.id}}&orderby=name&page={{page}}">按名称排序</a>
                            
                            {% if file.size == 0 %}
                                <a class="dropdown-option" style="color:red" href="javascript:remove('{{file.id}}', '{{file.name}}')">删除</a>
                            {% end %}
                          </div>
                        </div>
                    </div>
                {% else %}
                    {# 其他普通笔记 #}
                    <div class="float-right">
                        <a href="javascript:history.back()">返回</a>
                    </div>
                {% end %}
            {% end %}
        </div>
        {% end %}

        {% if error != "" and error != None%}
        <div class="col-md-12 error">
            {{error}}
        </div>
        {% end %}

        {% comment 如果有新类型的文件，继续增加file_type即可，后台实现不用变更 %}

        {% if file_type == "md" or file_type == "text" %}
            {% include note/editor/markdown.html %}
        {% elif file_type == "group" %}
            {% include note/note_list.html %}
        {% elif file_type == "csv" %}
            {% include note/editor/csv_editor.html %}
        {% elif file_type == "gallery" %}
            {% include note/editor/gallery.html %}
        {% elif file_type == "list" %}
            {% include note/editor/list.html %}
        {% else %}
            {% include note/editor/post.html %}
        {% end %}
    </div>

    {% init prev_note = None %}
    {% init next_note = None %}
    {% if prev_note or next_note %}
        <div class="card">
            {% if prev_note %}
                <div class="col-md-6"><a href="/note/view?id={{prev_note.id}}">[上一篇]{{prev_note.name}}</a></div>
            {% else %}
                <div class="col-md-6">[上一篇]无</div>
            {% end %}

            {% if next_note %}
                <div class="col-md-6"><a class="float-right" href="/note/view?id={{next_note.id}}">[下一篇]{{next_note.name}}</a></div>
            {% end %}
        </div>
    {% end %}

    {% if show_pagination and "page" in globals() %}
    <div class="card">
        {% include mod_pagenation.html %}
    </div>
    {% end %}

<script type="text/javascript">
function rename(id, oldName) {
    var newName = prompt("新名称", oldName);
    console.log(newName);
    if (newName != "" && newName != null) {
        $.post("/note/rename", {id:id, name:newName}, function (resp) {
            var code = resp.code;
            if (code != "success") {
                alert(resp.message);
            } else {
                // $("#file-"+id).text(newName);
                window.location.reload();
            }
        })
    }
}

function remove(id, name, parentId) {
    var confirmed = confirm("确定删除'" + name + "'?");
    if (confirmed) {
        $.post("/note/remove", {id:id}, function (resp) {
            var code = resp.code;
            if (code != "success") {
                alert(resp.message);
            } else {
                if (parentId) {
                    window.location.href = "/note/view?id="+parentId;
                } else {
                    window.location.href = "/";
                }
            }
        })
    }
}

$(function () {

    $(".dropdown").click(function (e) {
        var target = e.target;
        $(target).next(".dropdown-content").slideToggle("fast");
    });

    $("body").on("click", function (e) {
        var target = e.target;
        if ($(target).hasClass("dropdown") || $(target).hasClass("dropdown-btn")) {
            return;
        }
        $(".dropdown-content").hide();
    });

})

</script>

{% end %}

{% block aside %}
    {% include note/mod_aside.html %}
{% end %}
